/*
#####################################################################
####################	   T R I B U I T 		#####################
#####################################################################

Copyright (C) 2010  Christoph Schnack, Berlin (Germany)

This file is part of Tribuit.

    Tribuit is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Tribuit is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Tribuit.  If not, see <http://www.gnu.org/licenses/>.


########################################################################
*/
package Mainframe;

import java.awt.*;
import java.awt.event.*;
import java.io.File;

import javax.swing.*;

import GuiTools.*;
import PictureDescription.PictureStorage;
import Settings.*;
import Storage.Storage;
import SystemTray.TribuitSystemTray;

public class Mainframe extends TribuitFrame{
	Language LANG;
	Storage MAINVOC;
	Settings SET;
	Mainframe tf;
	public LookUpPanel luPanel;
	Component[] components;
	TribuitMenu MENU;
	TribuitSystemTray TRAY;
	CharTableButton ctb;
	WordTableButton wtb;
	ManageButton mb;
	WordCheckButton wb;
	int buttonY = 5, buttonX=294;
	
	public Mainframe(Storage stor,PictureStorage picsto,Settings s){
		super("Tribuit");
		tf=this;
		SET = s;
		LANG = SET.getLanguage();
		MAINVOC = stor;
		MENU = new TribuitMenu(MAINVOC,picsto,this,SET);
		TRAY = new TribuitSystemTray(MAINVOC,SET,tf);
		luPanel = new LookUpPanel(SET,MAINVOC,this);
		ctb = new CharTableButton();
		wtb = new WordTableButton();
		mb = new ManageButton();
		wb = new WordCheckButton();
		this.getContentPane().add(BorderLayout.CENTER,new TribuitCanvas());
        this.getContentPane().add(BorderLayout.SOUTH,luPanel);
        this.setJMenuBar(MENU);
        this.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
        this.addWindowListener(new WindowAdapter() {
		      public void windowClosing(WindowEvent e){
		          if(!SET.getAddToSystemTray())GuiTools.exitTribuit(MAINVOC,tf,SET);
		          else{
		        	  tf.setVisible(false);
		          }
		      }
		});
        /*this.addWindowFocusListener(new WindowFocusListener(){

			public void windowGainedFocus(WindowEvent arg0) {
				luPanel.searchTF.requestFocus();
			}

			public void windowLostFocus(WindowEvent arg0) {
				ctb.setVisible(false);
				wtb.setVisible(false);
				mb.setVisible(false);
				wb.setVisible(false);
			}
        	
        });*/
        if(SystemTray.isSupported() && SET.getAddToSystemTray()) TRAY.initSystemTray();
	}
	
	public void updateMainframe(){
		LANG = SET.getLanguage();
		LookAndFeel old = UIManager.getLookAndFeel();
		try {
			UIManager.setLookAndFeel( SET.getLookAndFeel() );
			SwingUtilities.updateComponentTreeUI(this);
		}catch( Exception e ) {
			try{
				UIManager.setLookAndFeel( old );
				SwingUtilities.updateComponentTreeUI(this);
			}catch(Exception f){f.printStackTrace();}
			e.printStackTrace();
		}
		if(SystemTray.isSupported()) TRAY.updateTray();
		MENU.updateLanguage();
		luPanel = new LookUpPanel(SET,MAINVOC,this);
		ctb.updateLanguage();
		wtb.updateLanguage();
		mb.updateLanguage();
		wb.updateLanguage();
		this.getContentPane().removeAll();
		this.getContentPane().add(new TribuitCanvas());
		this.getContentPane().add(BorderLayout.SOUTH,luPanel);
		this.pack();
		this.repaint();
	}
	
	public void updateMainframePane(Storage MAINVOC,Settings SET){
		this.getContentPane().removeAll();
		this.getContentPane().add(new TribuitCanvas());
		this.getContentPane().add(BorderLayout.SOUTH,new LookUpPanel(SET,MAINVOC,this));
		this.pack();
	}
	
	public TribuitMenu getMenu(){
		return MENU;
	}
	
	public class TribuitCanvas extends JPanel{
		Graphics g;
		int startx,sizeX=300,sizeY=300;
		JPanel panel;
		
		public TribuitCanvas(){
			super();
			panel=this;
			setLayout(null);
			setPreferredSize(new Dimension(sizeX,sizeY));
			panel.add(ctb);
			panel.add(wtb);
			panel.add(mb);
			panel.add(wb);
			panel.addMouseMotionListener(new MouseMotionListener(){
				public void mouseDragged(MouseEvent arg0) {}
				public void mouseMoved(MouseEvent arg){
					int y = arg.getY(),x = arg.getX();
					if((y<5) || (x<5) || (x>290)){
						ctb.setVisible(false);
						wtb.setVisible(false);
						mb.setVisible(false);
						wb.setVisible(false);
						return;
					}
					if(y<30){
						if(!ctb.isVisible()){
							ctb.setVisible(true);
							wtb.setVisible(true);
							mb.setVisible(true);
							wb.setVisible(true);
						}
					}
					else{
						ctb.setVisible(false);
						wtb.setVisible(false);
						mb.setVisible(false);
						wb.setVisible(false);
					}
				}
				
			});
			
		}
		
		public void paintComponent(Graphics g){
			super.paintComponent(g);
			Color bgColor = SET.getBackgroundColor();
			g.setColor(bgColor);
			g.fillRect(0, 0, 306, 306);
			startx = 65;
			int index=7,colorIndex=1;
			Color[] colorsOriginal = SET.getLogoColors();
			Color[] colors = SET.getLogoColors();
			for(int i=1;i<8;i++){
				if(i<5){
					colorIndex=i;
				}
				else{
					colorIndex--;
				}
				for(int r=0;r<7;r++){
					colors[r] = new Color(
							(Integer)Math.round(colorsOriginal[r].getRed()/4)*colorIndex,
							(Integer)Math.round(colorsOriginal[r].getGreen()/4)*colorIndex,
							(Integer)Math.round(colorsOriginal[r].getBlue()/4)*colorIndex
							);
				}
				//T
				g.setColor(colors[0]);
				if(i<5){
					g.drawLine(startx+18+i,137-i,startx+18+i,180-i);
					g.drawLine(startx+6+i,128+i,startx+38-i,128+i);
					g.drawLine(startx+5+i,136-index,startx+5+i,128+index);
					g.drawLine(startx+38-i,136-index,startx+38-i,128+index);
					g.drawLine(startx+18+i,181-i,startx+26-i,181-i);
				}
				else{
					g.drawLine(startx+18+i,137-index,startx+18+i,172+i);
					g.drawLine(startx+14-i,128+i,startx+26-i,128+i);
					g.drawLine(startx+18+i,128+i,startx+30+i,128+i);
				}

				//R
				g.setColor(colors[1]);
				if(i<5){
					g.drawLine(startx+40+i,150+i,startx+40+i,180-i);
					g.drawLine(startx+40+i,149+i,startx+54-i,149+i);
					g.drawLine(startx+40+i,181-i,startx+48-i,181-i);
					g.drawLine(startx+55-i,149+i,startx+55-i,157-i);
				}
				else{
					g.drawLine(startx+40+i,150+i,startx+40+i,172+i);
					g.drawLine(startx+40+i,149+i,startx+46+i,149+i);
				}
				
				//I
				g.setColor(colors[2]);
				if(i<5){
					g.drawLine(startx+60+i,138+i,startx+60+i,146-i);
					g.drawLine(startx+68-i,138+i,startx+68-i,146-i);
					g.drawLine(startx+60+i, 146-i, startx+68-i, 146-i);
					g.drawLine(startx+60+i, 138+i, startx+68-i, 138+i);
					
					g.drawLine(startx+60+i, 150+i, startx+60+i, 180-i);
					g.drawLine(startx+60+i, 149+i, startx+68-i, 149+i);
					g.drawLine(startx+60+i, 181-i, startx+68-i, 181-i);
				}
				else{
					g.drawLine(startx+60+i, 158-i, startx+60+i, 172+i);
				}
				
				//B
				g.setColor(colors[3]);
				if(i<5){
					g.drawLine(startx+75+i,129+i,startx+75+i,157);
					g.drawLine(startx+83-i,157-i,startx+92-index,157-i);
					g.drawLine(startx+75+i,128+i,startx+83-i,128+i);
				}
				else{
					g.drawLine(startx+75+i,137-i,startx+75+i,157-i);
					g.drawLine(startx+75+i,157-i,startx+92-index,157-i);
				}
				g.drawLine(startx+75+i,149+i,startx+75+i,180-i);
				g.drawLine(startx+85+i,149+index,startx+85+i,180-index);
				g.drawLine(startx+83-i,173+i,startx+92,173+i);

				//U
				g.setColor(colors[4]);
				if(i<5){
					g.drawLine(startx+100+i,150+i,startx+100+i,180-i);
					g.drawLine(startx+118-i,150+i,startx+118-i,180-i);
					g.drawLine(startx+100+i,149+i,startx+108-i,149+i);
					g.drawLine(startx+110+i,149+i,startx+118-i,149+i);
				}
				else{
					g.drawLine(startx+100+i,158-i,startx+100+i,180-i);
					g.drawLine(startx+118-i,158-i,startx+118-i,180-i);
				}
				g.drawLine(startx+108-i,173+i,startx+110+i,173+i);
				
				//I
				g.setColor(colors[5]);
				if(i<5){
					g.drawLine(startx+125+i,138+i,startx+125+i,146-i);
					g.drawLine(startx+133-i,138+i,startx+133-i,146-i);
					g.drawLine(startx+125+i, 146-i, startx+133-i, 146-i);
					g.drawLine(startx+125+i, 138+i, startx+133-i, 138+i);
					
					g.drawLine(startx+125+i, 150+i, startx+125+i, 180-i);
					g.drawLine(startx+125+i, 149+i, startx+133-i, 149+i);
					g.drawLine(startx+125+i, 181-i, startx+133-i, 181-i);
				}
				else{
					g.drawLine(startx+125+i, 158-i, startx+125+i, 172+i);
				}
				
				//T
				g.setColor(colors[6]);
				if(i<5){
					g.drawLine(startx+145+i, 129+i, startx+145+i, 137+i);
					g.drawLine(startx+140+i,138+i,startx+145+i,138+i);
					g.drawLine(startx+153-i,138+i,startx+158-i,138+i);
					g.drawLine(startx+145+i,146-i,startx+145+i,180-i);
					
					g.drawLine(startx+159-i,138+i,startx+159-i,146-i);
					g.drawLine(startx+139+i,138+i,startx+139+i,146-i);
					g.drawLine(startx+145+i, 128+i, startx+153-i, 128+i);
					g.drawLine(startx+145+i, 181-i, startx+153-i, 181-i);
				}
				else{
					g.drawLine(startx+145+i, 137-i, startx+145+i, 145-i);
					g.drawLine(startx+148-i,138+i,startx+153-i,138+i);
					g.drawLine(startx+145+i,138+i,startx+150+i,138+i);
					g.drawLine(startx+145+i,139+i,startx+145+i,172+i);
				}
				index--;
			}
		}
	}
	
	public class WordTableButton extends JPanel{
		WordTableButton button;
		boolean border = false;
		Image image =  GuiTools.loadImageFromGraphics("wordTableIcon.png");
		public WordTableButton(){
			super();
			button = this;
			button.setLayout(null);
			button.setBounds(buttonX-(2*32),buttonY,30,30);
			button.setToolTipText(LANG.getMenuWordTable());
			button.setOpaque(false);
			button.addMouseListener(new MouseListener(){
				public void mouseClicked(MouseEvent arg0) {
					MENU.fireWordTable();
				}
				public void mouseEntered(MouseEvent arg0) {
					border=true;
					button.repaint();
				}
				public void mouseExited(MouseEvent arg0) {
					border=false;
					button.repaint();
				}
				public void mousePressed(MouseEvent arg0) {
				}
				public void mouseReleased(MouseEvent arg0) {
				}
			});
			button.setVisible(false);
		}
		
		public void paintComponent(Graphics gr){
			Color c = SET.getBackgroundColor();
			float[] f = Color.RGBtoHSB(c.getRed(), c.getBlue(), c.getGreen(),null);
			if(f[2]>0.9)gr.setColor(Color.black);
			else gr.setColor(Color.white);
			if(border){
				gr.drawImage(image,5,5,null);
				gr.drawRect(0,0,29,29);
				return;
			}
			gr.drawImage(image,5,5,null);
		}
		
		public void updateLanguage(){
			button.setToolTipText(LANG.getMenuWordTable());
		}
	}
	public class WordCheckButton extends JPanel{
		WordCheckButton button;
		boolean border = false;
		Image image =  GuiTools.loadImageFromGraphics("wordcheckIcon.png");
		public WordCheckButton(){
			super();
			button = this;
			button.setLayout(null);
			button.setBounds(buttonX-(3*32),buttonY,30,30);
			button.setToolTipText(LANG.getMenuWordcheck());
			button.setOpaque(false);
			button.addMouseListener(new MouseListener(){
				public void mouseClicked(MouseEvent arg0) {
					MENU.fireWordcheck();
				}
				public void mouseEntered(MouseEvent arg0) {
					border=true;
					button.repaint();
				}
				public void mouseExited(MouseEvent arg0) {
					border=false;
					button.repaint();
				}
				public void mousePressed(MouseEvent arg0) {
				}
				public void mouseReleased(MouseEvent arg0) {
				}
			});
			button.setVisible(false);
		}
		
		public void paintComponent(Graphics gr){
			Color c = SET.getBackgroundColor();
			float[] f = Color.RGBtoHSB(c.getRed(), c.getBlue(), c.getGreen(),null);
			if(f[2]>0.9)gr.setColor(Color.black);
			else gr.setColor(Color.white);
			if(border){
				gr.drawImage(image,5,5,null);
				gr.drawRect(0,0,29,29);
				return;
			}
			gr.drawImage(image,5,5,null);
		}
		
		public void updateLanguage(){
			button.setToolTipText(LANG.getMenuWordcheck());
		}
	}
	
	public class ManageButton extends JPanel{
		ManageButton button;
		boolean border = false;
		Image image =  GuiTools.loadImageFromGraphics("manageIcon.png");
		public ManageButton(){
			super();
			button = this;
			button.setLayout(null);
			button.setBounds(buttonX-(4*32),buttonY,30,30);
			button.setToolTipText(LANG.getMenuManage());
			button.setOpaque(false);
			button.addMouseListener(new MouseListener(){
				public void mouseClicked(MouseEvent arg0) {
					MENU.fireVocabularyManager();
				}
				public void mouseEntered(MouseEvent arg0) {
					border=true;
					button.repaint();
				}
				public void mouseExited(MouseEvent arg0) {
					border=false;
					button.repaint();
				}
				public void mousePressed(MouseEvent arg0) {
				}
				public void mouseReleased(MouseEvent arg0) {
				}
			});
			button.setVisible(false);
		}
		
		public void paintComponent(Graphics gr){
			Color c = SET.getBackgroundColor();
			float[] f = Color.RGBtoHSB(c.getRed(), c.getBlue(), c.getGreen(),null);
			if(f[2]>0.9)gr.setColor(Color.black);
			else gr.setColor(Color.white);
			if(border){
				gr.drawImage(image,5,5,null);
				gr.drawRect(0,0,29,29);
				return;
			}
			gr.drawImage(image,5,5,null);
		}
		
		public void updateLanguage(){
			button.setToolTipText(LANG.getMenuManage());
		}
	}
	
	public class CharTableButton extends JPanel{
		CharTableButton button;
		boolean border = false;
		Image image =  GuiTools.loadImageFromGraphics("specialChar.png");
		public CharTableButton(){
			super();
			button = this;
			button.setLayout(null);
			button.setBounds(buttonX-(1*32),buttonY,30,30);
			button.setToolTipText(LANG.getSpecialCharacter());
			button.setOpaque(false);
			button.addMouseListener(new MouseListener(){
				public void mouseClicked(MouseEvent arg0) {
					MENU.fireCharTable();
				}
				public void mouseEntered(MouseEvent arg0) {
					border=true;
					button.repaint();
				}
				public void mouseExited(MouseEvent arg0) {
					border=false;
					button.repaint();
				}
				public void mousePressed(MouseEvent arg0) {
				}
				public void mouseReleased(MouseEvent arg0) {
				}
			});
			button.setVisible(false);
		}
		
		public void paintComponent(Graphics gr){
			Color c = SET.getBackgroundColor();
			float[] f = Color.RGBtoHSB(c.getRed(), c.getBlue(), c.getGreen(),null);
			if(f[2]>0.9)gr.setColor(Color.black);
			else gr.setColor(Color.white);
			if(border){
				gr.drawImage(image,5,5,null);
				gr.drawRect(0,0,29,29);
				return;
			}
			gr.drawImage(image,5,5,null);
		}
		
		public void updateLanguage(){
			button.setToolTipText(LANG.getSpecialCharacter());
		}
	}
}